package sm.upgrade;

import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sm.upgrade.api.UpgradePhase;
import sm.util.SMMap;

public class ExpireListUpgrade implements UpgradePhase {

	private static Logger logger = LoggerFactory.getLogger(UpgradePhase.class);

	private HibernateTemplate template;

	public void setTemplate(HibernateTemplate template) {
		this.template = template;
	}

	@Override
	public void upgrade() {
		logger.info("Expiring global lists...");
		List<Object> globallists = template.find("from globallists");
		if (globallists != null) {
			Calendar c = Calendar.getInstance();
			c.add(Calendar.HOUR_OF_DAY, -1);
			for (Object globallist : globallists) {
				SMMap globallistMap = (SMMap) globallist;
				globallistMap.put("expiration", c.getTime());
				template.update("globallists", globallistMap);
			}
		}

		logger.info("  Finished expiring global lists.");
	}

}
